window.onload= function () {

    var url = '/active/index',
        config = {
            elem: '#test'
            ,url:url
            ,cols: [[
                {field:'id', width:50, title: 'id', sort: true}
                ,{field:'active_name', width:200, title: '活动名称'}
                ,{field:'active_num', width:100, title: '成团人数', sort: true}
                ,{field:'begin_time', width:250, title: '有效期',templet: function (item) {
                        var start_time = timestampToTime(parseInt(item.begin_time));
                        var end_time =  timestampToTime(parseInt(item.end_time));
                        return start_time + '  --  '+ end_time;
                    }}
                ,{field:'status', width:120, title: '状态',templet: function (item) {
                        switch (parseInt(item.status)){
                            case 0:
                                return '暂停中';
                            case 1:
                                return '进行中';
                            case 2:
                                return '过期';
                            case 3:
                                return '禁用';
                        }
                    }}
                ,{field:'set_top', title: '是否置顶', width: 100, align:'center', templet:function(d){
                        var checked = d.set_top == 1 ? 'checked' : '';
                        return '<input type="checkbox" name="newsTop" data-id="'+d.id +'" '+checked+' lay-filter="newsTop" lay-skin="switch" lay-text="是|否" '+d.newsTop+'>'
                    }}
                ,{fixed: 'right', minWidth:150, title:'操作', align:'center', toolbar: '#barDemo'} //这里的toolbar值是模板元素的选择器

            ]]
            ,page: true
        };

    layui.use(['form','layer','jquery','table'], function(){
        var table = layui.table,
            form = layui.form,
            layer = layui.layer,
            $ = layui.jquery,
            operation = {
                "setTop":function (data) {
                    Utils.layer.closeAll();
                    Utils.layer.load(1,{'shade':0.5});
                    $.ajax({
                        url: '/active/set-top',
                        data:data,
                        method:'post',
                        dataType:'json',
                        success:function (data) {
                            Utils.layer.closeAll();
                            Utils.layer.msg(data.msg);
                            table.render(config);
                        }
                    });
                    return true;
                },
                "setType":function (data) {
                    Utils.layer.closeAll();
                    Utils.layer.load(1,{'shade':0.5});
                    $.ajax({
                        url: '/active/set-type',
                        data:data,
                        method:'post',
                        dataType:'json',
                        success:function (data) {
                            Utils.layer.closeAll();
                            Utils.layer.msg(data.msg);
                            table.render(config);
                        }
                    });
                    return true;
                },
                "getQrcode":function (data) {
                    Utils.layer.closeAll();
                    Utils.layer.load(1,{'shade':0.5});
                    $.ajax({
                        url: '/active/qrcode',
                        data:data,
                        method:'post',
                        dataType:'json',
                        success:function (data) {
                            Utils.layer.closeAll();
                            if(data.code == 200){
                                var img = data.data.cdnUrl;
                                //显示图片
                                Utils.layer.open({
                                    "content":[
                                        '<form class="layui-form" action="">',
                                            '<img src='+img+' style="width:220px;height:220px;padding:0 70px;"/>',
                                        '</form>'
                                    ].join(''),
                                    "btn":['确认'],
                                    "area":["400px","350px"],
                                    "yes":function () {
                                        Utils.layer.closeAll();
                                    }
                                });

                            }else{
                                Utils.layer.msg(data.msg);
                            }
                        }
                    });
                    return true;
                },


            };
        var active_type = $("#active_type").val();
        config.url = '/active/index?type='+active_type;
        table.render(config);
        //监听工具条
        table.on('tool(barDemo)', function(obj){ //注：tool是工具条事件名，test是table原始容器的属性 lay-filter="对应的值"
            var data = obj.data; //获得当前行数据
            var layEvent = obj.event; //获得 lay-event 对应的值（也可以是表头的 event 参数对应的值）
            var tr = obj.tr; //获得当前行 tr 的DOM对象
            switch (layEvent){
                case 'detail':
                    location.href = '/active/active-info?id='+data.id;
                    break;
                case 'set-type':
                    Utils.layer.open({
                        "content":[
                            '<form class="layui-form" action="">',
                            '<div class="layui-form-item">\n' +
                            '    <label class="layui-form-label">选择框</label>\n' +
                            '    <div class="layui-input-block">\n' +
                            '      <select name="status" id="status" lay-verify="required">\n' +
                            '           <option value="0">普通</option>\n' +
                            '           <option value="1">限时团</option>\n' +
                            '           <option value="2">店长推荐</option>\n' +
                            '           <option value="3">热门</option>\n' +
                            '           <option value="4">新品</option>' +
                            '      </select>\n' +
                            '    </div>\n' +
                            '  </div>',
                            '</form>'
                        ].join(''),
                        "btn":['确认','取消'],
                        "area":["400px","350px"],
                        "yes":function () {
                            var type = $('#status').val();
                            operation.setType({
                                'id':data.id,
                                'type':type
                            });
                        },
                        "btn2":function () {
                            Utils.layer.closeAll();
                        }
                    });
                    form.render();
                    break;
                case 'qrcode':
                    operation.getQrcode({
                        'id':data.id
                    });
                    break;
            }
        });
        //是否置顶
        form.on('switch(newsTop)', function(obj){
            var top = 0;
            if($(obj.elem).is(':checked')){
                top = 1;
            }
            var data = obj.elem;
            var id = data.getAttribute('data-id');
            operation.setTop({
                id: id,
                set_top: top
            })
        });

        //搜索
        form.on("submit(serch-form)",function(data){
            var query = $("#form1").serialize();
            var url = '/active/index?'+ query;
            config.url = url;
            table.render(config);
            return false;
        });

    });
};
